
/**
 * @class
 * @constructor
 */
function Application() {
	
	var genefinder = null;
	
	var flyted = null;
	
	var bdgp = null;
	
	this.initGenefinder = function() {
		
		// initialise genefinder widget

		log("instantiate a service for the genefinder widget");
		var service = new flyui.flybase.Service("../../../data/flybase");
		
		log("instantiate a renderer for the genefinder widget");
		var renderPane = document.getElementById("genefinderWidget");
		var renderer = new flyui.genefinder.DefaultRenderer();
		renderer.setCanvas(renderPane);
		
		log("instantiate a genefinder widget");
		genefinder = new flyui.genefinder.Widget(service, renderer);

	};
	
	this.initFlyted = function() {
		
		// initialise flyted image widget
		log("instantiate a service for the flyted image widget");
		var service = new flyui.flyted.Service("../../../data/flyted");
		
		log("instantiate a renderer for the flyted image widget");
		var renderPane = document.getElementById("flytedImageWidget");
		var renderer = new flyui.flyted.ImageWidget.DefaultRenderer(renderPane);
		
		log("instantiate a flyted image widget");
		flyted = new flyui.flyted.ImageWidget(service, renderer);
		
	};
	
	this.initBdgp = function() {
		
		// initialise bdgp image widget
		log("instantiate a service for the bdgp image widget");
		var service = new flyui.bdgp.Service("../../../data/bdgp");
		
		log("instantiate a renderer for the bdgp image widget");
		var renderPane = document.getElementById("bdgpImageWidget");
		var renderer = new flyui.bdgp.ImageWidget.DefaultRenderer(renderPane);
		
		log("instantiate a bdgp image widget");
		bdgp = new flyui.bdgp.ImageWidget(service, renderer);
		
	};

	this.init = function() {
		
		var logReader = new YAHOO.widget.LogReader("logger");

		this.initGenefinder();
		
		this.initFlyted();
		
		this.initBdgp();

		// attach gene selection listener to genefinder
		genefinder.subscribe("GENESELECTED", this.onGeneSelected, null);

		// attach form submission listener to form
		log("hook form submission to widget call");
		YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
		
		log("application loaded, showing main pane");
		flyui.mvcutils.hide(document.getElementById("loadingPane"));
		flyui.mvcutils.show(document.getElementById("applicationPane"));
	};
	
	this.onGeneSelected = function(type, args) {
		var gene = args[0];
		log("gene selected: "+gene+" "+gene.flybaseID);
		flyted.findImagesByFlybaseGeneID(gene.flybaseID);
		bdgp.findImagesByAnyGeneLabel(gene.flybaseID);
	};	
	
	this.onFormSubmitted = function(event) {
		log("form submitted");
		var query = document.getElementById("queryInput").value;
		var caseSensitive = document.getElementById("caseSensitiveInput").checked;
		log("case sensitive: "+caseSensitive);
		genefinder.findGenesByAnyName(query, caseSensitive);
	};
	
	this.init();
}

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new Application();});